#include "Avalon/ServiceLocatorTests/MockServiceLocatorClientComponents.hpp"
#include <boost/lexical_cast.hpp>
#include <cppunit/extensions/HelperMacros.h>
#include "Avalon/IOTests/MockClientChannel.hpp"
#include "Avalon/Serialization/BinaryDataShuttleFactory.hpp"
#include "Avalon/ServiceLocator/ServiceLocatorClient.hpp"
#include "Avalon/ServiceLocatorTests/MockServiceLocatorDataStore.hpp"
#include "Avalon/ServiceLocatorTests/MockServiceLocatorServerComponents.hpp"

using namespace Avalon;
using namespace Avalon::IO;
using namespace Avalon::IO::Tests;
using namespace Avalon::Serialization;
using namespace Avalon::ServiceLocator;
using namespace Avalon::ServiceLocator::Tests;
using namespace boost;
using namespace boost::posix_time;
using namespace std;

MockServiceLocatorClientComponents::MockServiceLocatorClientComponents(
    MockServiceLocatorServerComponents* server)
    : m_server(server) {
  m_clientChannel = new MockClientChannel(m_server->m_serverConnection);
  m_client.reset(new ServiceLocatorClient(m_clientChannel,
    new BinaryDataShuttleFactory()));
}

MockServiceLocatorClientComponents::~MockServiceLocatorClientComponents() {}

MockClientChannel& MockServiceLocatorClientComponents::GetClientChannel() {
  return *m_clientChannel;
}

ServiceLocatorClient& MockServiceLocatorClientComponents::GetClient() {
  return *m_client;
}

void MockServiceLocatorClientComponents::EstablishValidConnection() {
  string username = "user" + lexical_cast<string>(rand());
  string password = "password";
  unsigned int accountId;
  CPPUNIT_ASSERT_NO_THROW(accountId =
    m_server->m_dataStore->CreateAccount(username,
    DirectoryEntry::Tag(DirectoryEntry::Type::DIRECTORY, 0, "*")));
  CPPUNIT_ASSERT_NO_THROW(m_server->m_dataStore->SetPassword(accountId,
    ComputeSHA(lexical_cast<string>(accountId) + password)));
  CPPUNIT_ASSERT_NO_THROW(m_client->Login(username, password));
}
